package servlet;

import user.Book;
import util.JDBC;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        request.getSession().setAttribute("username", username);
        request.getSession().setAttribute("password", password);
        ConnectServlet conn = new ConnectServlet();
        conn.doGet(request, response);
        Connection connection = conn.getConnection(); //获取连接
        if (connection == null) {
            response.getWriter().println("用户名或者密码不对（3秒后自动跳转）");
            response.setHeader("Refresh", "3;url=login.jsp");
        } else {
            request.getRequestDispatcher("search.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}